package com.qf.servlet;

import com.qf.pojo.PageBean;
import com.qf.pojo.Profile;
import com.qf.service.ProfileService;
import com.qf.service.impl.ProfileServiceImpl;
import com.qf.utils.StrUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 29125
 * @version V1.0
 * @Project java2203
 * @Package com.qf.servlet
 * @Description:
 * @Date 2022/5/27 14:06
 */
@WebServlet("/list")
public class ListPage extends BaseServlet {
    public void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询时输入中文，出现乱码，避免出现乱码
        request.setCharacterEncoding("utf-8");
        //1获取前端数据
        String pageNum = request.getParameter("pageNum");//显示当前页码和每页显示条数
        String pageSize = request.getParameter("pageSize");

        //非空验证
        int page_num = 1;
        int page_size = 2;
        try {
            if (!StrUtils.isEmpty(pageNum)) {
                page_num = Integer.parseInt(pageNum);
                if (page_num < 1) {
                    page_num = 1;//当输入搜索页码小于1时默认为页码1，直接显示第一页
                }
            }
            if (!StrUtils.isEmpty(pageSize)) {
                page_size = Integer.parseInt(pageSize);
                if (page_size < 1) {
                    page_size = 2;//当输入每页条数小于1时，不成立默认给的每页8条
                }
            }
        } catch (Exception e) {//改为Exception
            e.printStackTrace();
        }
        try {
            //2调用业务逻辑
            ProfileService profileService=new ProfileServiceImpl();
            PageBean<Profile> pageBean=profileService.findByPage(page_num,page_size);
            System.out.println(pageBean);
            
            //3转发重定向
            request.setAttribute("pageBean", pageBean);
            request.getRequestDispatcher("/list.jsp").forward(request, response);
        } catch (Exception e) {
            //3转发重定向
            request.getRequestDispatcher("/message.jsp").forward(request, response);
        }

    }
}
